package cn.net.yugu.doraemon.netty.utils;

import java.security.MessageDigest;

/**
 * @description:
 * @author: wanghongli
 * @create: 2023-03-10 14:09:52
 */
public class CodecUtils {
    static final String PASSWORD_SALT = "67884E9%^&*67899A26C18DC28";
    static final String ACCESS_SECRET = "67884E9%^&*67899A26C18DC28";
    static final String kEY_LOGIN = "67884E9%^&*67899A26C18DC28";
    /**
     * 创建客户的时候调用该方法
     * @param src
     * @return
     */
    public static String password(String src) {
        return md5(PASSWORD_SALT + sha(PASSWORD_SALT + src));
    }

    public static String signMd5(String src) {
        return md5(src + ACCESS_SECRET);
    }

    public static byte[]  signMd5ForByte(String src) {
        byte[] signByte = null;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            signByte = md.digest((src + ACCESS_SECRET).getBytes("utf-8"));
        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        }
        return signByte;
    }

    public static String sha(String src) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("SHA");
            byte[] a = md.digest(src.getBytes("utf-8"));
            return byteToHex(a);

        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        }
    }

    public static String md5(String src) {
        MessageDigest md;
        try {
            md = MessageDigest.getInstance("MD5");
            byte[] a = md.digest(src.getBytes("utf-8"));
            return byteToHex(a);

        } catch (Exception e) {
            e.printStackTrace();
            throw new IllegalArgumentException(e);
        }
    }

    public static String byteToHex(byte[] a) {
        StringBuilder buf = new StringBuilder();

        for (int i = 0; i < a.length; i++) {
            if (a[i] < 0)
                buf.append(Integer.toHexString(a[i]&0xff));
            else if (a[i] < 16) {
                buf.append('0');
                buf.append(Integer.toHexString(a[i]));
            } else {
                buf.append(Integer.toHexString(a[i]));
            }
        }

        return buf.toString().toUpperCase();
    }

    public static byte[] hexToByte(String hexStr) {
        if (hexStr.length() < 1)
            return null;
        byte[] result = new byte[hexStr.length()/2];
        for (int i = 0;i< hexStr.length()/2; i++) {
            int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
            int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 使用异或进行加密
     */
    public static String XORencode(String res) {
        byte[] bs = res.getBytes();
        for (int i = 0; i < bs.length; i++) {
            bs[i] = (byte) ((bs[i]) ^ ACCESS_SECRET.hashCode());
        }
        return byteToHex(bs);
    }


    /**
     *  使用异或进行解密
     */
    public static String XORdecode(String res) {
        byte[] bs = hexToByte(res);
        for (int i = 0; i < bs.length; i++) {
            bs[i] = (byte) ((bs[i]) ^ ACCESS_SECRET.hashCode());
        }
        return new String(bs);
    }

    public static String keyLoginMd5(String src) {
        return md5(src + kEY_LOGIN);
    }
    public static String keyLoginSha(String src) {
        return sha(src + kEY_LOGIN);
    }
}
